/*
 * @Description: 过滤器
 * @Author: lijinpeng
 * @Date: 2020-07-26 22:48:14
 * @LastEditors: lijinpeng
 */ 
import dayjs from 'dayjs'
/**
 * @descripttion: 日期格式化
 * @param {*} date
 * @param {*} format
 */
export const dateformat = (date, format) => {
  if (date) {
    if (Number(date)) {
      return dayjs(Number(date)).format(format || 'YYYY-MM-DD HH:mm:ss')
    }
    try {
      return dayjs(date).format(format || 'YYYY-MM-DD HH:mm:ss')
    } catch (err) {
      return date
    }
  }
}

/**
 * @descripttion: 金额格式化
 * @param {Number} money 金额
 * @param {Number} point 小数
 * @return: 
 */
export const filterMoney = (money, point) => {
  if (isNaN(money) || money == null) money = '0.00'
  let sign = money == (money = Math.abs(money))
  money = Math.floor(money * 100 + 0.50000000001)
  let cents = money % 100
  money = Math.floor(money / 100).toString()
  if (cents < 10) cents = '0' + cents
  for (var i = 0; i < Math.floor((money.length - (1 + i)) / 3); i++) money = money.substring(0, money.length - (4 * i + 3)) + ',' + money.substring(money.length - (4 * i + 3))
  if (point === 0) {
    if (cents === '00') {
      return (sign ? '' : '-') + money
    }
  }

  return (sign ? '' : '-') + money + '.' + cents
}

export default {
  dateformat,
  filterMoney
}